package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.bean.TaskListBean;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;
import com.jshx.sz.gdjt.control.HeaderView;
import com.jshx.sz.gdjt.thread.TodoLocalThread;
import com.jshx.sz.gdjt.util.ProgressDialogUtil;
import com.zijunlin.Zxing.Demo.CaptureActivity;

/**
 * 工具列表界面
 * @author caxiang
 * @time 2013-08-21
 */
public class TodoListActivity extends SuperActivity 
{
	
	/**
	 * 标题栏组件
	 */
	private HeaderView header;
	
	/**
	 * 列表组件
	 */
	private ListView todoList;
	
	/**
	 * 组件实例化工具
	 */
	private LayoutInflater layoutInflater;
	
	//
	private EditText todolist_queryinput;
	
	/**
	 * 工具数据容器
	 */
	private ArrayList data = new ArrayList();
	
	/**
	 * 
	 */
	private String type;
	
	/**
	 * 
	 */
	private ImageButton todolist_erweima;
	
	/**
	 * 页面句柄
	 */
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
			int what = msg.what;
			super.handleMessage(msg);
			ProgressDialogUtil.dismiss();
			if(what == ApplicationGlobal.data_obtained)
			{
				myAdapter.notifyDataSetChanged();
			}
			else if(what == ApplicationGlobal.data_missed)
			{
				//setLocalData();
				myAdapter.notifyDataSetChanged();
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todolist_activity);
		TAG = "TodoListActivity";
		layoutInflater = LayoutInflater.from(this);
		type = getIntent().getStringExtra("type");
		findView();
		initView();
	}

	/**
	 * 实例化页面组件
	 */
	private void findView() {
		header = (HeaderView)findViewById(R.id.todolist_header);
		todoList = (ListView)findViewById(R.id.todolist);
		todolist_erweima = (ImageButton)findViewById(R.id.todolist_erweima);
		todolist_queryinput = (EditText)findViewById(R.id.todolist_queryinput);
	}

	/**
	 * 初始化页面数据和页面组件
	 */
	private void initView() {
		todolist_erweima.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
//				Intent i = new Intent(TodoListActivity.this, CaptureActivity.class);
//				startActivity(i);
//				Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//				intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
//				startActivityForResult(intent, 10);//跳转至系统自带拍照界面
				
				
				Intent intent = new Intent(TodoListActivity.this, CaptureActivity.class);
				startActivityForResult(intent, 0);
				
			}
		});
		todoList.setAdapter(myAdapter);
	}
	
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		if(requestCode == 10 && resultCode == RESULT_OK)
//		{
//			todolist_queryinput.setText("A016387524445");
//		}
//		super.onActivityResult(requestCode, resultCode, data);
//	}

	Object objerwei;
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(resultCode == RESULT_OK)
		{
			String QR_CODE=intent.getStringExtra("QR_CODE");
			String  ltemp=QR_CODE.substring(QR_CODE.indexOf("TEL:")+4);
			String  rtemp=ltemp.substring(0,QR_CODE.indexOf(";")-1);
			todolist_queryinput.setText(rtemp);
			
			if(data != null && data.size() > 0)
			{
				objerwei=data.get(0);
				data.clear();
			}
			todoList.setAdapter(myAdapter);
		}
		//super.onActivityResult(requestCode, resultCode, intent);
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		if(data != null)
		{
			data.clear();
		}
		String STATUS_SQL="";
		String PERSON_SQL="";
		String userId=ApplicationGlobal.userId;
		if(type.equals("check"))
		{
			header.setTitle("待检查");
			STATUS_SQL=" AND status='1'";
			PERSON_SQL=" AND check_person='"+userId+"'";
		}
		else if(type.equals("examine"))
		{
			header.setTitle("待审核");
			STATUS_SQL=" AND status='2'";
			PERSON_SQL=" AND audit_person='"+userId+"'";
		} 
		else if(type.equals("checkHis"))
		{
			header.setTitle("检查历史");
			STATUS_SQL=" AND status='2'";
			PERSON_SQL=" AND check_person='"+userId+"'";
		} 
		else if(type.equals("examineHis"))
		{
			header.setTitle("审核历史");
			STATUS_SQL=" AND status='3'";
			PERSON_SQL=" AND audit_person='"+userId+"'";
		} 
		header.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				TodoListActivity.this.finish();
			}
		}, R.drawable.backbtn_select, "返回");
		
		
		//网络获取数据
//		String userId=ApplicationGlobal.userId;
//		String params= ",'userId':'"+userId+"'";
//		if(status!=null&&!status.equals(""))
//		{
//			params+=status;
//		}
//		ProgressDialogUtil.show(this, "数据加载中...", true);
//		TodoThread thread=new TodoThread(handler, params,data);
//		thread.start();
		//本地获取数据
		
		String params= " WHERE 1=1 "+STATUS_SQL+PERSON_SQL;
		
		// AND audit_person=''  "+status;
		
		if(objerwei==null)
		{
			ProgressDialogUtil.show(this, "数据加载中...", true);
			TodoLocalThread thread=new TodoLocalThread(handler, params,data);
			thread.start();
		}
		else
		{
			data.clear();
			data.add(objerwei);
			myAdapter.notifyDataSetChanged();
		}
		
	}

	ArrayList childList=new ArrayList();
	
	/**
	 * listVIew数据适配器
	 */
	private BaseAdapter myAdapter = new BaseAdapter()
	{
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			HashMap map=(HashMap)data.get(position);
			convertView = layoutInflater.inflate(R.layout.todo_list_item, null);
			TextView num = (TextView)convertView.findViewById(R.id.todo_list_item_num);
			TextView state = (TextView)convertView.findViewById(R.id.todo_list_item_state);
			num.setText(map.get("operationCode")+"");
			state.setText(map.get("status")+"");
			convertView.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) {
					HashMap map=(HashMap)data.get(position);
					childList=(ArrayList)map.get("child");
					String operationCode = (String)map.get("order1");
					if(childList != null && childList.size() == 1)
					{
						HashMap childMap=(HashMap) childList.get(0);
						String dataNumber = (String)childMap.get("dataNumber");
						String orderNo = (String)childMap.get("operationCode");
						String teamId = (String)childMap.get("teamId");
						String assetCode = (String)childMap.get("status");
						
						
						Intent intent = new Intent();
						if(dataNumber.equals("1"))
						{
							intent = new Intent(TodoListActivity.this, TaskCheckActivity.class);
						}
						else if(dataNumber.equals("4"))
						{
							intent = new Intent(TodoListActivity.this, TaskActivityGroup.class);
						}
						
						intent.putExtra("orderNo", orderNo);
						intent.putExtra("assetCode", assetCode);
						intent.putExtra("type", type);
						intent.putExtra("teamId", teamId);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(TodoListActivity.this, PopActivity.class);
						intent.putExtra("isImage", false);
						intent.putExtra("isNeedShowCheckBox", false);
						intent.putExtra("type", type);
						TaskListBean lb=new TaskListBean();
						lb.setreturnList( childList);
						intent.putExtra("taskList", lb);
						intent.putExtra("operatonCode", operationCode);
						startActivityForResult(intent, 0);
					}
				}
			});
			 
			
			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			if(data == null)
			{
				return null;
			}
			return data.get(position);
		}
		
		@Override
		public int getCount() {
			if(data == null)
			{
				return 0;
			}
			return data.size();
		}
	};
	
}
